package com.ruoyi.syrw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.syrw.domain.bo.RefReportExcelBo;
import com.ruoyi.syrw.domain.vo.RefReportExcelVo;
import com.ruoyi.syrw.domain.RefReportExcel;
import com.ruoyi.syrw.mapper.RefReportExcelMapper;
import com.ruoyi.syrw.service.IRefReportExcelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * report与excel关系Service业务层处理
 *
 * @author zyb
 * @date 2024-04-30
 */
@RequiredArgsConstructor
@Service
public class RefReportExcelServiceImpl implements IRefReportExcelService {

    private final RefReportExcelMapper baseMapper;

    /**
     * 查询report与excel关系
     */
    @Override
    public RefReportExcelVo queryById(Long reportId){
        return baseMapper.selectVoById(reportId);
    }

    /**
     * 查询report与excel关系列表
     */
    @Override
    public TableDataInfo<RefReportExcelVo> queryPageList(RefReportExcelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RefReportExcel> lqw = buildQueryWrapper(bo);
        Page<RefReportExcelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询report与excel关系列表
     */
    @Override
    public List<RefReportExcelVo> queryList(RefReportExcelBo bo) {
        LambdaQueryWrapper<RefReportExcel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RefReportExcel> buildQueryWrapper(RefReportExcelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RefReportExcel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getReportId() != null, RefReportExcel::getReportId, bo.getReportId());
        lqw.eq(bo.getExcelTypeId() != null, RefReportExcel::getExcelTypeId, bo.getExcelTypeId());
        lqw.eq(bo.getExcelId() != null, RefReportExcel::getExcelId, bo.getExcelId());
        return lqw;
    }

    /**
     * 新增report与excel关系
     */
    @Override
    public Boolean insertByBo(RefReportExcelBo bo) {
        RefReportExcel add = BeanUtil.toBean(bo, RefReportExcel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReportId(add.getReportId());
        }
        return flag;
    }

    /**
     * 修改report与excel关系
     */
    @Override
    public Boolean updateByBo(RefReportExcelBo bo) {
        RefReportExcel update = BeanUtil.toBean(bo, RefReportExcel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RefReportExcel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除report与excel关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
